<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Cate;

class InfoController extends Controller
{
    
    public function index(){
    	// $page = DB::table('info')->get();
    	$page = DB::table('category')->join('model', 'category.model_id', '=', 'model.model_id')->select('category.cate_id','category.parent_id','category.name', 'model.model_name')->paginate(20);
    	// 转换为数组
    	$list=$page->toArray();
    	// 无限级分类
    	$listArr=tree(array_map('get_object_vars',$list['data']));
		return view('admin.info.index',['list'=>$listArr,'page'=>$page]);
    }
    public function model(Request $request,$id){
    	if($request->isMethod('get')){
    		if($id){
    			$type=DB::table('category')->select('model_id','cate_id','name')->where('cate_id', $id)->get()->toArray();
    			switch ($type[0]->model_id) {
    				case 2:
    					$page = DB::table('info')->select('info_id','creattime','title')->where('cate_id', $id)->paginate(20);
						return  view('admin.info.alist',['res'=>$type[0],'page'=>$page]);
    					break;
    				case 3:
    					$this->picture();
    					break;
    				case 4:
    					$this->links();
    					break;
    				case 5:
    					$this->videos();
    					break;
    				case 6:
    					$this->downs();
    					break;
    				case 7:
    					$this->products();
    					break;
    				case 8:
    					$this->singlepages();
    					break;
    				default: 
    					return  abort('404');
    					break;
    			}
    		}else{
				return  abort('404');
    		}
		}
    }
    public function add(Request $request,$id=1){
    	if($request->isMethod('get')){
    		// 获取栏目数组
    		$category=DB::select('select cate_id,name,parent_id from blog_category');
    		$category=tree(array_map('get_object_vars',$category));
    		$cate=DB::select('select cate_id,name from blog_category where cate_id='.$id);
			return  view('admin.info.article',['cate'=>$category,'cate'=>$cate[0]]);
		}else{
			// 插入数据 返回插入数据的bool值
			$this->validate($request, [
		        'title' => 'required',
		        'cate_id' => 'required',
		        'zhaiyao' => 'required',
		        'content' => 'required',
		        'from' => 'required',
		        'picture' => 'required',
		        'keywords' => 'required',
		        'description' => 'required',
		        'creattime' => 'required',
		        'author' => 'required',
		        'hit' => 'required'
    		],
    		['required'=>':attribute 必填项'
    		],
    		['title'=>'标题',
	        'cate_id' => '栏目',
	        'zhaiyao' => '摘要',
	        'content' => '内容',
	        'from' => '信息来源',
	        'picture' => '缩略图',
	        'keywords' => '关键词',
	        'description' => '描述',
	        'creattime' => '创建时间',
	        'author' => '作者',
	        'hit' => '点击次数'

    		]);
			$request->input('is_url', 'null');
			$request->input('InfoAttachment', 'null');
			$request->input('edittime', 'null');
			$request->input('url', 'null');
			$res = DB::table('info')->insert($request->except ('_token'));
			if($res){
				return redirect('info/index')->with('success','成功');
			}else{
				echo '输入有误，后期优化用户体验！';
			}
		}
    }
    public function edit(Request $request,$id){
    	if($request->isMethod('get')){
    		// 获取栏目数组
    		$category=DB::select('select cate_id,name,parent_id from blog_category');
    		$category=tree(array_map('get_object_vars',$category));
    		$info=DB::select('select * from blog_info where info_id=5');
			return  view('admin.info.editarticle',['cate'=>$category,'info'=>$info[0]]);
		}else{
			// 插入数据 返回插入数据的bool值
			$this->validate($request, [
		        'info_id' => 'required',
		        'title' => 'required',
		        'cate_id' => 'required',
		        'zhaiyao' => 'required',
		        'content' => 'required',
		        'from' => 'required',
		        'picture' => 'required',
		        'keywords' => 'required',
		        'description' => 'required',
		        'creattime' => 'required',
		        'author' => 'required',
		        'hit' => 'required'
    		],
    		['required'=>':attribute 必填项'
    		],
    		['title'=>'标题',
	        'cate_id' => '栏目',
	        'info_id' => 'id',
	        'zhaiyao' => '摘要',
	        'content' => '内容',
	        'from' => '信息来源',
	        'picture' => '缩略图',
	        'keywords' => '关键词',
	        'description' => '描述',
	        'creattime' => '创建时间',
	        'author' => '作者',
	        'hit' => '点击次数'

    		]);
			$request->input('is_url', 'null');
			$request->input('InfoAttachment', 'null');
			$request->input('edittime', 'null');
			$request->input('url', 'null');
			$res = DB::table('info')->where('info_id',$request->input('info_id'))->update($request->except ('_token'));
			if($res){
				return redirect('info/index')->with('success','成功');
			}else{
				echo '输入有误，后期优化用户体验！';
			}
		}
    }













}
